home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / FAST_LEX / FLEX.1 < prev    next >
Text File  |  1988-07-04  |  16KB  |  573 lines

  1. .TH FLEX 1 "13 May 1987"
  2. .SH NAME
  3. flex - fast lexical analyzer generator
  4. .SH SYNOPSIS
  5. .B flex
  6. [
  7. .B -dfirstvFILT -c[efmF] -Sskeleton_file
  8. ] [ 
  9. .I filename
  10. ]
  11. .SH DESCRIPTION
  12. .I flex
  13. is a rewrite of
  14. .I lex
  15. intended to right some of that tool's deficiencies: in particular,
  16. .I flex
  17. generates lexical analyzers much faster, and the analyzers use
  18. smaller tables and run faster.
  19. .SH OPTIONS
  20. In addition to lex's
  21. .B -t
  22. flag, flex has the following options:
  23. .TP
  24. .B -d
  25. makes the generated scanner run in
  26. .I debug
  27. mode.  Whenever a pattern is recognized the scanner will
  28. write to
  29. .I stderr
  30. a line of the form:
  31. .nf
  32.  
  33.     --accepting rule #n
  34.  
  35. .fi
  36. Rules are numbered sequentially with the first one being 1.
  37. .TP
  38. .B -f
  39. has the same effect as lex's -f flag (do not compress the scanner
  40. tables); the mnemonic changes from
  41. .I fast compilation
  42. to (take your pick)
  43. .I full table
  44. or
  45. .I fast scanner.
  46. The actual compilation takes
  47. .I longer,
  48. since flex is I/O bound writing out the big table.
  49. .IP
  50. This option is equivalent to
  51. .B -cf
  52. (see below).
  53. .TP
  54. .B -i
  55. instructs flex to generate a
  56. .I case-insensitive
  57. scanner.  The case of letters given in the flex input patterns will
  58. be ignored, and the rules will be matched regardless of case.  The
  59. matched text given in
  60. .I yytext
  61. will have the preserved case (i.e., it will not be folded).
  62. .TP
  63. .B -r
  64. specifies that the scanner uses the
  65. .B REJECT
  66. action.
  67. .TP
  68. .B -s
  69. causes the
  70. .I default rule
  71. (that unmatched scanner input is echoed to
  72. .I stdout)
  73. to be suppressed.  If the scanner encounters input that does not
  74. match any of its rules, it aborts with an error.  This option is
  75. useful for finding holes in a scanner's rule set.
  76. .TP
  77. .B -v
  78. has the same meaning as for lex (print to
  79. .I stderr
  80. a summary of statistics of the generated scanner).  Many more statistics
  81. are printed, though, and the summary spans several lines.  Most
  82. of the statistics are meaningless to the casual flex user.
  83. .TP
  84. .B -F
  85. specifies that the
  86. .ul
  87. fast
  88. scanner table representation should be used.  This representation is
  89. about as fast as the full table representation
  90. .ul
  91. (-f),
  92. and for some sets of patterns will be considerably smaller (and for
  93. others, larger).  In general, if the pattern set contains both "keywords"
  94. and a catch-all, "identifier" rule, such as in the set:
  95. .nf
  96.  
  97.     "case"    return ( TOK_CASE );
  98.     "switch"  return ( TOK_SWITCH );
  99.     ...
  100.     "default" return ( TOK_DEFAULT );
  101.     [a-z]+    return ( TOK_ID );
  102.  
  103. .fi
  104. then you're better off using the full table representation.  If only
  105. the "identifier" rule is present and you then use a hash table or some such
  106. to detect the keywords, you're better off using
  107. .ul
  108. -F.
  109. .IP
  110. This option is equivalent to
  111. .B -cF
  112. (see below).
  113. .TP
  114. .B -I
  115. instructs flex to generate an
  116. .I interactive
  117. scanner.  Normally, scanners generated by flex always look ahead one character
  118. before deciding that a rule has been matched.  At the possible cost of some
  119. scanning overhead (it's not clear that more overhead is involved), flex will
  120. generate a scanner which only looks ahead when needed.  Such scanners are
  121. called
  122. .I interactive
  123. because if you want to write a scanner for an interactive system such
  124. as a command shell, you will probably want the user's input to be terminated
  125. with a newline, and without
  126. .B -I
  127. the user will have to type a character in addition to the newline in order
  128. to have the newline recognized.  This leads to dreadful interactive performance.
  129. .IP
  130. If all this seems to confusing, here's the general rule: if a human will
  131. be typing in input to your scanner, use
  132. .B -I,
  133. otherwise don't; if you don't care about how fast your scanners run and
  134. don't want to make any assumptions about the input to your scanner,
  135. always use
  136. .B -I.
  137. .IP
  138. Note,
  139. .B -I
  140. cannot be used in conjunction with
  141. .I full
  142. or
  143. .I fast tables,
  144. i.e., the
  145. .B -f, -F, -cf,
  146. or
  147. .B -cF
  148. flags.
  149. .TP
  150. .B -L
  151. instructs flex to not generate
  152. .B #line
  153. directives (see below).
  154. .TP
  155. .B -T
  156. makes flex run in
  157. .I trace
  158. mode.  It will generate a lot of messages to standard out concerning
  159. the form of the input and the resultant non-deterministic and deterministic
  160. finite automatons.  This option is mostly for use in maintaining flex.
  161. .TP 
  162. .B -c[efmF]
  163. controls the degree of table compression.
  164. .B -ce
  165. directs flex to construct
  166. .I equivalence classes,
  167. i.e., sets of characters
  168. which have identical lexical properties (for example, if the only
  169. appearance of digits in the flex input is in the character class
  170. "[0-9]" then the digits '0', '1', ..., '9' will all be put
  171. in the same equivalence class).
  172. .B -cf
  173. specifies that the
  174. .I full
  175. scanner tables should be generated - flex should not compress the
  176. tables by taking advantages of similar transition functions for
  177. different states.
  178. .B -cF
  179. specifies that the alternate fast scanner representation (described
  180. above under the
  181. .B -F
  182. flag)
  183. should be used.
  184. .B -cm
  185. directs flex to construct
  186. .I meta-equivalence classes,
  187. which are sets of equivalence classes (or characters, if equivalence
  188. classes are not being used) that are commonly used together.
  189. A lone
  190. .B -c
  191. specifies that the scanner tables should be compressed but neither
  192. equivalence classes nor meta-equivalence classes should be used.
  193. .IP
  194. The options
  195. .B -cf
  196. or
  197. .B -cF
  198. and
  199. .B -cm
  200. do not make sense together - there is no opportunity for meta-equivalence
  201. classes if the table is not being compressed.  Otherwise the options
  202. may be freely mixed.
  203. .IP
  204. The default setting is
  205. .B -cem
  206. which specifies that flex should generate equivalence classes
  207. and meta-equivalence classes.  This setting provides the highest
  208. degree of table compression.  You can trade off
  209. faster-executing scanners at the cost of larger tables with
  210. the following generally being true:
  211. .nf
  212.  
  213.     slowest            smallest
  214.                -cem
  215.                -ce
  216.                -cm
  217.                -c
  218.                -c{f,F}e
  219.                -c{f,F}
  220.     fastest            largest
  221.  
  222. .fi
  223. .TP
  224. .B -Sskeleton_file
  225. overrides the default skeleton file from which flex constructs
  226. its scanners.  You'll never need this option unless you are doing
  227. flex maintenance or development.
  228. .SH INCOMPATIBILITIES WITH LEX
  229. .I flex
  230. is fully compatible with
  231. .I lex
  232. with the following exceptions:
  233. .IP -
  234. There is no run-time library to link with.  You needn't
  235. specify
  236. .I -ll
  237. when linking, and you must supply a main program.  (Hacker's note: since
  238. the lex library contains a main() which simply calls yylex(), you actually
  239. .I can
  240. be lazy and not supply your own main program and link with
  241. .I -ll.)
  242. .IP -
  243. lex's
  244. .B %r
  245. (Ratfor scanners) and
  246. .B %t
  247. (translation table) options
  248. are not supported.
  249. .IP -
  250. The do-nothing
  251. .ul
  252. -n
  253. flag is not supported.
  254. .IP -
  255. When definitions are expanded, flex encloses them in parentheses.
  256. With lex, the following
  257. .nf
  258.  
  259.     NAME    [A-Z][A-Z0-9]*
  260.     %%
  261.     foo{NAME}?      printf( "Found it\\n" );
  262.     %%
  263.  
  264. .fi
  265. will not match the string "foo" because when the macro
  266. is expanded the rule is equivalent to "foo[A-Z][A-Z0-9]*?"
  267. and the precedence is such that the '?' is associated with
  268. "[A-Z0-9]*".  With flex, the rule will be expanded to
  269. "foo([A-z][A-Z0-9]*)?" and so the string "foo" will match.
  270. .IP -
  271. .B yymore()
  272. is not supported.
  273. .IP -
  274. The undocumented lex-scanner internal variable
  275. .B yylineno
  276. is not supported.
  277. .IP -
  278. If your input uses
  279. .B REJECT,
  280. you must run flex with the
  281. .B -r
  282. flag.  If you leave out the flag, the scanner will abort at run-time
  283. with a message that the scanner was compiled without the flag being
  284. specified.
  285. .IP -
  286. The
  287. .B input()
  288. routine is not redefinable, though may be called to read characters
  289. following whatever has been matched by a rule.  If
  290. .B input()
  291. encounters and end-of-file the normal
  292. .B yywrap()
  293. processing is done.  A ``real'' end-of-file is returned as
  294. .I EOF.
  295. .IP
  296. Input can be controlled by redefining the
  297. .B YY_INPUT
  298. macro.
  299. YY_INPUT's calling sequence is "YY_INPUT(buf,result,max_size)".  Its
  300. action is to place up to max_size characters in the character buffer "buf"
  301. and return in the integer variable "result" either the
  302. number of characters read or the constant YY_NULL (0 on Unix systems)
  303. systems) to indicate EOF.  The default YY_INPUT reads from the
  304. file-pointer "yyin" (which is by default
  305. .I stdin),
  306. so if you
  307. just want to change the input file, you needn't redefine
  308. YY_INPUT - just point yyin at the input file.
  309. .IP
  310. A sample redefinition of YY_INPUT (in the first section of the input
  311. file):
  312. .nf
  313.  
  314.     %{
  315.     #undef YY_INPUT
  316.     #define YY_INPUT(buf,result,max_size) \\
  317.         result = (buf[0] = getchar()) == EOF ? YY_NULL : 1;
  318.     %}
  319.  
  320. .fi
  321. You also can add in things like counting keeping track of the
  322. input line number this way; but don't expect your scanner to
  323. go very fast.
  324. .IP -
  325. .B output()
  326. is not supported.
  327. Output from the ECHO macro is done to the file-pointer
  328. "yyout" (default
  329. .I stdout).
  330. .IP -
  331. Trailing context is restricted to patterns which have either
  332. a fixed-sized leading part or a fixed-sized trailing part.
  333. For example, "a*/b" and "a/b*" are okay, but not "a*/b*".
  334. This restriction is due to a bug in the trailing context
  335. algorithm given in
  336. .I Principles of Compiler Design
  337. (and
  338. .I Compilers - Principles, Techniques, and Tools)
  339. which can result in mismatches.  Try the following lex program
  340. .nf
  341.  
  342.     %%
  343.     x+/xy           printf( "I found \\"%s\\"\\n", yytext );
  344.  
  345. .fi
  346. on the input "xxy".  (If anyone knows of a fast algorithm for
  347. finding the beginning of trailing context for an arbitrary
  348. pair of regular expressions, please let me know!)
  349. If you must have arbitrary trailing context, you can use
  350. .B yyless()
  351. to effect it.
  352. .IP -
  353. flex reads only one input file, while lex's input is made
  354. up of the concatenation of its input files.
  355. .SH ENHANCEMENTS
  356. .IP -
  357. .I Exclusive start-conditions
  358. can be declared by using
  359. .B %x
  360. instead of
  361. .B %s.
  362. These start-conditions have the property that when they are active,
  363. .I no other rules are active.
  364. Thus a set of rules governed by the same exclusive start condition
  365. describe a scanner which is independent of any of the other rules in
  366. the flex input.  This feature makes it easy to specify "mini-scanners"
  367. which scan portions of the input that are syntactically different
  368. from the rest (e.g., comments).
  369. .IP -
  370. flex dynamically resizes its internal tables, so directives like "%a 3000"
  371. are not needed when specifying large scanners.
  372. .IP -
  373. The scanning routine generated by flex is declared using the macro
  374. .B YY_DECL.
  375. By redefining this macro you can change the routine's name and
  376. its calling sequence.  For example, you could use:
  377. .nf
  378.  
  379.     #undef YY_DECL
  380.     #define YY_DECL float lexscan( a, b ) float a, b;
  381.  
  382. .fi
  383. to give it the name
  384. .I lexscan,
  385. returning a float, and taking two floats as arguments.
  386. .IP -
  387. flex generates
  388. .B #line
  389. directives mapping lines in the output to
  390. their origin in the input file.
  391. .IP -
  392. You can put multiple actions on the same line, separated with
  393. semi-colons.  With lex, the following
  394. .nf
  395.  
  396.     foo    handle_foo(); return 1;
  397.  
  398. .fi
  399. is truncated to
  400. .nf
  401.  
  402.     foo    handle_foo();
  403.  
  404. .fi
  405. flex does not truncate the action.  Actions that are not enclosed in
  406. braces are terminated at the end of the line.
  407. .IP -
  408. Actions can be begun with
  409. .B %{
  410. and terminated with
  411. .B %}.
  412. In this case, flex does not count braces to figure out where the
  413. action ends - actions are terminated by the closing
  414. .B %}.
  415. This feature is useful when the enclosed action has extraneous
  416. braces in it (usually in comments or inside inactive #ifdef's)
  417. that throw off the brace-count.
  418. .IP -
  419. All of the scanner actions (e.g.,
  420. .B ECHO, yywrap ...)
  421. except the
  422. .B unput()
  423. and
  424. .B input()
  425. routines,
  426. are written as macros, so they can be redefined if necessary
  427. without requiring a separate library to link to.
  428. .SH FILES
  429. .TP
  430. .I flex.skel
  431. skeleton scanner
  432. .TP
  433. .I flex.fastskel
  434. skeleton scanner for -f and -F
  435. .TP
  436. .I flexskelcom.h
  437. common definitions for skeleton files
  438. .TP
  439. .I flexskeldef.h
  440. definitions for compressed skeleton file
  441. .TP
  442. .I fastskeldef.h
  443. definitions for -f, -F skeleton file
  444. .SH "SEE ALSO"
  445. .LP
  446. lex(1)
  447. .LP
  448. M. E. Lesk and E. Schmidt,
  449. .I LEX - Lexical Analyzer Generator
  450. .SH AUTHOR
  451. Vern Paxson, with the help of many ideas and much inspiration from
  452. Van Jacobson.  Original version by Jef Poskanzer.  Fast table
  453. representation is a partial implementation of a design done by Van
  454. Jacobson.  The implementation was done by Kevin Gong and Vern Paxson.
  455. .LP
  456. Thanks to the many flex beta-testers, especially Casey Leedom,
  457. Nick Christopher, Chris Faylor, Eric Goldman, Craig Leres, Mohamed el Lozy,
  458. Esmond Pitt, Jef Poskanzer, and Dave Tallman.  Thanks to John Gilmore,
  459. Bob Mulcahy,
  460. Rich Salz, and Richard Stallman for help with various distribution headaches.
  461. .LP
  462. Send comments to:
  463. .nf
  464.  
  465.      Vern Paxson
  466.      Real Time Systems
  467.      Bldg. 46A
  468.      Lawrence Berkeley Laboratory
  469.      1 Cyclotron Rd.
  470.      Berkeley, CA 94720
  471.  
  472.      (415) 486-6411
  473.  
  474.      vern@lbl-{csam,rtsg}.arpa
  475.      ucbvax!lbl-csam.arpa!vern
  476.  
  477. .fi
  478. .SH DIAGNOSTICS
  479. .LP
  480. .I flex scanner jammed -
  481. a scanner compiled with
  482. .B -s
  483. has encountered an input string which wasn't matched by
  484. any of its rules.
  485. .LP
  486. .I flex input buffer overflowed -
  487. a scanner rule matched a string long enough to overflow the
  488. scanner's internal input buffer (as large as
  489. .B BUFSIZ
  490. in "/usr/include/stdio.h").  You can edit
  491. .I flexskelcom.h
  492. and increase
  493. .B YY_BUF_SIZE
  494. and
  495. .B YY_MAX_LINE
  496. to increase this limit.
  497. .LP
  498. .I REJECT used and scanner was
  499. .I not generated using -r -
  500. just like it sounds.  Your scanner uses
  501. .B REJECT.
  502. You must run flex on the scanner description using the
  503. .B -r
  504. flag.
  505. .LP
  506. .I old-style lex command ignored -
  507. the flex input contains a lex command (e.g., "%n 1000") which
  508. is being ignored.
  509. .SH BUGS
  510. .LP
  511. Use of unput() or input() trashes the current yytext and yyleng.
  512. .LP
  513. Use of unput() to push back more text than was matched can
  514. result in the pushed-back text matching a beginning-of-line ('^')
  515. rule even though it didn't come at the beginning of the line.
  516. .LP
  517. Nulls are not allowed in flex inputs or in the inputs to
  518. scanners generated by flex.  Their presence generates fatal
  519. errors.
  520. .LP
  521. Do not mix trailing context with the '|' operator used to
  522. specify that multiple rules use the same action.  That is,
  523. avoid constructs like:
  524. .nf
  525.  
  526.         foo/bar      |
  527.         bletch       |
  528.         bugprone     { ... }
  529.  
  530. .fi
  531. They can result in subtle mismatches.  This is actually not
  532. a problem if there is only one rule
  533. using trailing context and it is the first in the list (so the
  534. above example will actually work okay).  The
  535. problem is due to fall-through in the action switch statement,
  536. causing non-trailing-context rules to execute the
  537. trailing-context code of their fellow rules.  This should
  538. be fixed, as it's a nasty bug and not obvious.  The proper fix is
  539. for flex to spit out a FLEX_TRAILING_CONTEXT_USED #define and then
  540. have the backup logic in a separate table which is consulted for
  541. each rule-match, rather than as part of the rule action.  The
  542. place to do the tweaking is in add_accept() - any kind soul want
  543. to be a hero?
  544. .LP
  545. The pattern:
  546. .nf
  547.  
  548.     x{3}
  549.  
  550. .fi
  551. is considered to be variable-length for the purposes of trailing
  552. context, even though it has a clear fixed length.
  553. .LP
  554. Due to both buffering of input and read-ahead, you cannot intermix
  555. calls to, for example,
  556. .B getchar()
  557. with flex rules and expect it to work.  Call
  558. .B input()
  559. instead.
  560. .LP
  561. The total table entries listed by the
  562. .B -v
  563. flag excludes the number of table entries needed to determine
  564. what rule has been matched.  The number of entries is equal
  565. to the number of DFA states if the scanner was not compiled
  566. with
  567. .B -r,
  568. and greater than the number of states if it was.
  569. .LP
  570. The scanner run-time speeds have not been optimized as much
  571. as they deserve.  Van Jacobson's work shows that the can go quite
  572. a bit faster still.
  573.